/*
 * Copyright © Yan Zhenjie. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sql.azarias.http;

import android.content.Context;

import com.sql.azarias.R;
import com.sql.azarias.views.ToastView;
import com.sql.azarias.views.dialog.ShapeLoadingDialog;
import com.yanzhenjie.nohttp.error.NetworkError;
import com.yanzhenjie.nohttp.error.NotFoundCacheError;
//import com.yanzhenjie.nohttp.error.ParseError;
import com.yanzhenjie.nohttp.error.TimeoutError;
import com.yanzhenjie.nohttp.error.URLError;
import com.yanzhenjie.nohttp.error.UnKnownHostError;
import com.yanzhenjie.nohttp.rest.OnResponseListener;
import com.yanzhenjie.nohttp.rest.Response;

import java.net.ProtocolException;
/**
 * Created by SQLong on 2017/2/7.
 */
public class DefaultResponseListener<T> implements OnResponseListener<Result<T>> {
    private Context mActivity;
    /**
     * Dialog.
     */
    private ShapeLoadingDialog shapeLoadingDialog;
    private HttpListener<T> httpListener;
    private AbstractRequestNew<T> abstractRequest;

    public DefaultResponseListener(Context mActivity , HttpListener<T> httpListener, AbstractRequestNew<T> abstractRequest) {
        this.mActivity = mActivity;
        this.httpListener = httpListener;
        this.abstractRequest = abstractRequest;
        if (mActivity != null) {
            shapeLoadingDialog = new ShapeLoadingDialog(mActivity);

        }
    }

    @Override
    public void onStart(int what) {
        // TODO 显示dialog。
        if (shapeLoadingDialog != null/* && !shapeLoadingDialog.isFinishing() && !shapeLoadingDialog.isShowing()*/)

            shapeLoadingDialog.show();
    }

    @Override
    public void onSucceed(int what, Response<Result<T>> response) {
        // http层的请求成功，响应码可能是200、400、500。
        if (httpListener != null && !abstractRequest.isCanceled())
            httpListener.onSucceed(what, response.get());
    }

    @Override
    public void onFailed(int what, Response<Result<T>> response) {
        Exception exception = response.getException();
        if (exception instanceof NetworkError) {// 网络不好
            ToastView.getInstance().show(R.string.error_please_check_network,mActivity);
        } else if (exception instanceof TimeoutError) {// 请求超时
            ToastView.getInstance().show(R.string.error_timeout,mActivity);
        } else if (exception instanceof UnKnownHostError) {// 找不到服务器
            ToastView.getInstance().show(R.string.error_not_found_server,mActivity);
        } else if (exception instanceof URLError) {// URL是错的
            ToastView.getInstance().show(R.string.error_url_error,mActivity);
        } else if (exception instanceof NotFoundCacheError) {
            // 这个异常只会在仅仅查找缓存时没有找到缓存时返回
            ToastView.getInstance().show(R.string.error_not_found_cache,mActivity);
        }else {
            ToastView.getInstance().show(R.string.error_unknow,mActivity);
        }
        if (httpListener != null && !abstractRequest.isCanceled())
            httpListener.onFailed(what);
    }

    @Override
    public void onFinish(int what) {
        // TODO 关闭dialog。
        if (shapeLoadingDialog != null/* && shapeLoadingDialog.isShowing()*/)
            shapeLoadingDialog.dismiss();

        if (httpListener != null)
            httpListener.onFinish(what);

    }
}
